#  /**
#  * Copyright (c) 2022 Beijing Jiaotong University
#  * PhotLab is licensed under [Open Source License].
#  * You can use this software according to the terms and conditions of the [Open Source License].
#  * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
#  *
#  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
#  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
#  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#  *
#  * See the [Open Source License] for more details.
#  */
#  /**
#  * Author: Zheng Wang
#  * Created: Jun. 27, 2022
#  * Supported by: National Key Research and Development Program of China
#  */

import inspect
from phot_engine.globals import ModuleAnalysisError


class ModuleAnalyzer:
    """
        模块分析类
        主要功能:
            1. 根据字符串寻找模块中的类
            2. 根据字符串寻找模块中的成员方法或函数
            3. 获取模块所有类信息
        Module analysis class
        Main functions:
        1. Find the class in the module based on the string
        2. Find the member methods or functions in the module based on the string
        3. Obtain information about all modules
    """

    def __init__(self, module_name):
        self.module_name = module_name

    # 找出模块里所有的类名
    def get_all_classes_name(self):
        classes = []
        if inspect.ismodule(self.module_name):
            class_members = inspect.getmembers(self.module_name, inspect.isclass)
            for (name, _) in class_members:
                classes.append(name)
            return classes
        else:
            raise ModuleAnalysisError("{} is not a module".format(self.module_name))

    # 找出模块里所有的函数名
    def get_all_functions_name(self):
        classes = []
        if inspect.ismodule(self.module_name):
            class_members = inspect.getmembers(self.module_name, inspect.isfunction)
            for (name, _) in class_members:
                classes.append(name)
            return classes
        else:
            raise ModuleAnalysisError("{} is not a module".format(self.module_name))


if __name__ == '__main__':
    import phot

    ma = ModuleAnalyzer(phot)
    print(ma.get_all_classes_name())
    print(ma.get_all_functions_name())
